'use strict';

app.service('goodsTypeService',['$http','$q','utilService',function($http,$q,utilService){
    var _this_=this;
    _this_.findAll=function($scope){
        var deferred = $q.defer();

        utilService.get({
            url:$scope.app.domain + 'chain/type'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

    _this_.getAllName=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'chain/type/all/name'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }


    _this_.findbyid=function($scope,id){
        var deferred = $q.defer();

        utilService.get({
            url:$scope.app.domain + 'chain/type/'+$scope.typeImgVO.id
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }


    //添加分类
    _this_.addTypes=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"chain/type",
            data:$scope.typeAddVO
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //编辑分类
    _this_.editTypes=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"chain/type",
            data:$scope.typeEditVO,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //删除分类
    _this_.deleteType=function($scope,id){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/type/'+$scope.modal.data.id,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //保存图片
    _this_.saveImg=function($scope,id){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/type/img',
            data:$scope.typeImgVO,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //删除图片
    _this_.delImg=function($scope,id){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/type/img',
            data:{id:$scope.typeImgVO.id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


}]);


